package vista;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

import negocio.beans.central.Proveedor;
import negocio.beans.central.RemitoProveedor;
import xml.XMLClass;
import controlador.CasaCentral;



public class ProveedoresAlta extends javax.swing.JFrame {

	
	private static final long serialVersionUID = 1L;
	private JTextField txtCUIT;
	private JTextField txtRazonSocial;
	private JTextField txtCondicionCompra;
	private JLabel lblCondicionCompra;
	private JLabel lblXML;
	private JTextField txtXmlName;
	private JButton btnCargar;
	private JButton btnAceptar;
	private JTextField txtTipo;
	private JTextField txtDireccion;
	private JLabel lblTipo;
	private JLabel lblDireccion;
	private JLabel lblRazonSocial;
	private JLabel lblCUIT;
	private JTable tblProveedores;

	public JTable getTblProveedores() {
		return tblProveedores;
	}

	public void setTblProveedores(JTable tblProveedores) {
		this.tblProveedores = tblProveedores;
	}

	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ProveedoresAlta inst = new ProveedoresAlta();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public ProveedoresAlta() {
		super();
		initGUI();
		CentrarJFrame();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setBackground(new java.awt.Color(202,203,251));
			getContentPane().setLayout(null);
			this.setTitle("Alta Proveedor");
			this.setResizable(false);
			{
				txtCUIT = new JTextField();
				getContentPane().add(txtCUIT);
				txtCUIT.setBounds(141, 30, 138, 21);
			}
			{
				txtRazonSocial = new JTextField();
				getContentPane().add(txtRazonSocial);
				txtRazonSocial.setBounds(141, 63, 138, 21);
			}
			{
				lblCUIT = new JLabel();
				getContentPane().add(lblCUIT);
				lblCUIT.setText("CUIT:");
				lblCUIT.setBounds(7, 30, 79, 14);
			}
			{
				lblRazonSocial = new JLabel();
				getContentPane().add(lblRazonSocial);
				lblRazonSocial.setText("Razon Social:");
				lblRazonSocial.setBounds(7, 63, 92, 14);
			}
			{
				lblDireccion = new JLabel();
				getContentPane().add(lblDireccion);
				lblDireccion.setText("Direccion:");
				lblDireccion.setBounds(7, 94, 79, 14);
			}
			{
				lblTipo = new JLabel();
				getContentPane().add(lblTipo);
				lblTipo.setText("Tipo:");
				lblTipo.setBounds(7, 127, 79, 14);
			}
			{
				lblXML = new JLabel();
				getContentPane().add(lblXML);
				lblXML.setText("XML:");
				lblXML.setBounds(7, 193, 79, 14);
			}
			{
				txtDireccion = new JTextField();
				getContentPane().add(txtDireccion);
				txtDireccion.setBounds(141, 94, 138, 21);
			}
			{
				txtTipo = new JTextField();
				getContentPane().add(txtTipo);
				txtTipo.setBounds(141, 127, 138, 21);
			}
			{
				txtXmlName = new JTextField();
				getContentPane().add(txtXmlName);
				txtXmlName.setBounds(141, 193, 138, 21);
			}
			{
				lblCondicionCompra = new JLabel();
				getContentPane().add(lblCondicionCompra);
				lblCondicionCompra.setText("Condicion Compra:");
				lblCondicionCompra.setBounds(7, 161, 134, 16);
			}
			{
				txtCondicionCompra = new JTextField();
				getContentPane().add(txtCondicionCompra);
				txtCondicionCompra.setBounds(141, 158, 138, 23);
			}
			{
				btnCargar = new JButton();
				getContentPane().add(btnCargar);
				btnCargar.setText("Cargar");
				btnCargar.setBounds(290, 193, 100, 21);
				btnCargar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						
						XMLClass xml = new XMLClass();
						Proveedor r = xml.AltaProveedor(txtXmlName.getText());
					
						txtCUIT.setText(r.getCuit());
						txtRazonSocial.setText(r.getRazonSocial());
						txtDireccion.setText(r.getDireccion());
						txtTipo.setText(r.getTipoProveedor());
						txtCondicionCompra.setText(r.getCondicionCompra());
					}
				});
			}
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar);
				btnAceptar.setText("Aceptar");
				btnAceptar.setBounds(105, 250, 99, 21);
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						
						CasaCentral.getInstancia().altaProveedor(txtCUIT.getText(), txtRazonSocial.getText(), txtDireccion.getText(), txtTipo.getText(),txtCondicionCompra.getText());
						
						DefaultTableModel temp = (DefaultTableModel) tblProveedores.getModel();						
						Object nuevo[]= {txtRazonSocial.getText()};
						temp.addRow(nuevo);
					
						txtCUIT.setText("");
						txtRazonSocial.setText("");
						txtDireccion.setText("");
						txtTipo.setText("");
						txtCondicionCompra.setText("");
					}
				});
			}
			pack();
			this.setSize(400, 330);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void CentrarJFrame(){
		
	    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	    
	    
	    int w = this.getSize().width;
	    int h = this.getSize().height;
	    int x = (dim.width-w)/2;
	    int y = (dim.height-h)/2;
	    
	    
	    this.setLocation(x, y);
	}
	

}
